//
//  NSArray+ZLCategory.m
//  ZLCategory
//
//  Created by zhaozl on 2022/3/7.
//

#import "NSArray+ZLCategory.h"

@implementation NSArray (ZLCategory)

- (NSData *)zl_JSONData {
    return [NSJSONSerialization dataWithJSONObject:self options:0 error:nil];
}

- (NSString *)zl_JSONString {
    return [[NSString alloc] initWithData:[self zl_JSONData] encoding:NSUTF8StringEncoding];
}

- (id)zl_safeObjectAtIndex:(NSUInteger)index {
    if (index < self.count) {
        return [self objectAtIndex:index];
    } else {
        return nil;
    }
}

@end

@implementation NSMutableArray (Utility)

- (void)zl_safeInsertObject:(id)object atIndex:(NSInteger)index {
    if (index < self.count && object) {
        [self insertObject:object atIndex:index];
    }
}

- (void)zl_safeAddObject:(id)object {
    if (object) {
        [self addObject:object];
    }
}

@end
